package br.hm.operandroid.activity;

import java.text.DecimalFormat;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import br.hm.operandroid.GenericGroup;
import br.hm.operandroid.R;
import br.hm.operandroid.manager.ContactTelephoneOperatorManager;
import br.hm.operandroid.manager.ContactTelephoneOperatorManagerFactory;
import br.hm.operandroid.manager.OperatorUtils;
import br.hm.telephoneoperator.CallOperatorException;

public class CountContactOperatorActivity extends ListActivity {

	private ContactTelephoneOperatorManager contactTelephoneOperatorManager;

	private Handler handler;

	private DecimalFormat decimalFormat = new DecimalFormat("##0.00");

	private GenericAdapter<GenericGroup> genericAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		handler = new Handler();
		this.contactTelephoneOperatorManager = ContactTelephoneOperatorManagerFactory
				.getNewInstance(this);

		GenericFormatter<GenericGroup> imageFormatter = new GenericFormatter<GenericGroup>() {

			@Override
			public void formatter(GenericGroup value, View view) {
				ImageView imageView = (ImageView) view;
				imageView.setImageResource(OperatorUtils
						.getImageResource(value.name));
			}

		};

		GenericFormatter<GenericGroup> operatorFormatter = new GenericFormatter<GenericGroup>() {
			@Override
			public void formatter(GenericGroup value, View view) {
				TextView textView = (TextView) view;
				textView.setText(value.name);
			}
		};

		GenericFormatter<GenericGroup> countFormatter = new GenericFormatter<GenericGroup>() {
			@Override
			public void formatter(GenericGroup value, View view) {
				TextView textView = (TextView) view;
				textView.setText(value.currentValue + " contatos");
			}
		};

		GenericFormatter<GenericGroup> percentFormatter = new GenericFormatter<GenericGroup>() {
			@Override
			public void formatter(GenericGroup value, View view) {
				TextView textView = (TextView) view;
				double percent = ((double) value.currentValue * 100)
						/ value.fullValue;
				textView.setText(decimalFormat.format(percent) + "%");
			}
		};

		genericAdapter = new GenericAdapter<GenericGroup>(this, imageFormatter,
				operatorFormatter, countFormatter, percentFormatter);

		View view = getLayoutInflater().inflate(R.layout.layout_title_relatory,
				null);

		TextView textTitle = (TextView) view
				.findViewById(R.id.title_relatory_text);
		textTitle.setText(getResources().getText(
				R.string.countcontact_rel_information));

		Button buttonTitle = (Button) view
				.findViewById(R.id.title_relatory_button_start);
		buttonTitle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				load();
			}
		});

		getListView().addHeaderView(view);

		setListAdapter(genericAdapter);

	}

	public void load() {
		new Thread(new CountContactOperatorRunnable()).start();
	}

	protected void showToast(final String result) {
		handler.post(new Runnable() {

			@Override
			public void run() {
				Toast.makeText(CountContactOperatorActivity.this, result,
						Toast.LENGTH_LONG).show();
			}
		});

	}

	class CountContactOperatorRunnable implements Runnable {

		private ProgressDialog progressDialog;

		public CountContactOperatorRunnable() {
			progressDialog = ProgressDialog.show(
					CountContactOperatorActivity.this, "Operadora",
					"Buscando Operadora, favor aguardar...", false, true);
		}

		@Override
		public void run() {
			try {
				List<GenericGroup> genericGroups = contactTelephoneOperatorManager
						.countContactOperator();
				genericAdapter.list = genericGroups;
				handler.post(new Runnable() {
					@Override
					public void run() {
						genericAdapter.notifyDataSetChanged();
					}
				});
			} catch (CallOperatorException e) {
				showToast("Problema ao solicitar dados da operadora");
			} finally {
				closeProgressDialog();
			}
		}

		protected void closeProgressDialog() {
			handler.post(new Runnable() {
				@Override
				public void run() {
					if (progressDialog.isShowing()) {
						progressDialog.dismiss();
					}
				}
			});
		}

	}
}
